﻿using AutoMapper.Internal.Mappers;
using Microsoft.AspNetCore.Mvc;
using SMARTHOTEL.hotel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace SMARTHOTEL.RoomNum
{

    /// <summary>
    /// 房号管理
    /// </summary>
    [ApiExplorerSettings(GroupName = "v2")]
    public class RoomNumService:ApplicationService,IRoomNumService
    {
        IRepository<RoomNumber> roomRep;

        IRepository<RoomType> roomtyp;

        public RoomNumService(IRepository<RoomNumber> roomRep, IRepository<RoomType> roomtyp)
        {
            this.roomRep = roomRep;
            this.roomtyp = roomtyp;
        }
        /// <summary>
        /// 添加房号
        /// </summary>
        /// <param name="roomNumDto"></param>
        /// <returns></returns>
        public async Task<ResultDto<bool>> CreateAllAsync(CreateUpdateRoomNumDto roomNumDto)
        {
            try
            {
                var roomTypeList = (await roomtyp.GetListAsync()).FirstOrDefault(m=>m.RoomTypeName == roomNumDto.RoomTypeName);

                RoomNumber roomnum= ObjectMapper.Map<CreateUpdateRoomNumDto, RoomNumber>(roomNumDto);

                roomnum.RoomTypeId= roomTypeList.Id;

                await roomRep.InsertAsync(roomnum);

                return new ResultDto<bool> { Data = true, Code = StatusCode.Success, Msg = "添加成功" };
            }
            catch (Exception ex)
            {
                return new ResultDto<bool> { Data = false, Code = StatusCode.Faield, Msg = "添加失败"+ex.Message };
            }
        }
        /// <summary>
        /// 显示所有房号和房型名称
        /// </summary>
        /// <param name="roomTypeName"></param>
        /// <param name="roomNum"></param>
        /// <returns></returns>
        public Task<ResultDto<List<RoomNumDto>>> GetAllAsync(string? roomTypeName, string? roomNum)
        {
            var list = from a in roomRep.GetListAsync().Result
                       join b in roomtyp.GetListAsync().Result on a.RoomTypeId equals b.Id where a.IsDeleted == false
                       select new RoomNumDto
                       {
                           RoomTypeId = b.Id,
                           RoomTypeName = b.RoomTypeName,
                           RoomNum = a.RoomNum,
                           Sort = a.Sort,
                           Remark = a.Remark,
                           RoomNumberStatues = a.RoomNumberStatues,
                           IsDeleted = a.IsDeleted
                       };
            if (!string.IsNullOrEmpty(roomTypeName))
            {
                list = list.Where(a => a.RoomTypeName.Contains(roomTypeName));
            }
            if (!string.IsNullOrEmpty(roomNum))
            {
                list = list.Where(a => a.RoomNum.Contains(roomNum));
            }
            return Task.FromResult(new ResultDto<List<RoomNumDto>> { Data = list.ToList(), Code = StatusCode.Success, Msg = "查询成功" });
        }
        /// <summary>
        /// 批量修改房间类型（也可以批量修改上架下架）
        /// </summary>
        /// <returns></returns>
        public async Task<ResultDto<bool>> UpdateStateAsync(string Id)
        {
            try
            {
                var ids = Id.Split(',');
                foreach (var item in ids)
                {
                    var roomtype = await roomRep.GetAsync(x => x.Id == Guid.Parse(item));
                    if (roomtype != null)
                    {
                        roomtype.RoomNumberStatues = false;
                        await roomRep.UpdateAsync(roomtype);
                    }
                }
                return new ResultDto<bool> { Data = true, Code = StatusCode.Success, Msg = "修改成功" };
            }
            catch (Exception ex)
            {
                return new ResultDto<bool> { Data = false, Code = StatusCode.Faield, Msg = "修改失败" + ex.Message };
            }
        }
        /// <summary>
        /// 批量删除房间类型（也可以单个删除）
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<ResultDto<bool>> DeleteAllAsync(string Id)
        {
            try
            {
                var ids = Id.Split(",");
                foreach (var item in ids)
                {
                    var roomtype = await roomRep.GetAsync(x => x.Id == Guid.Parse(item));
                    if (roomtype != null)
                    {
                        roomtype.IsDeleted = true;
                        await roomRep.UpdateAsync(roomtype);
                    }
                }
                return new ResultDto<bool> { Data = false, Code = StatusCode.Faield, Msg = "删除成功" };

            }
            catch (Exception ex)
            {
                return new ResultDto<bool> { Data = false, Code = StatusCode.Faield, Msg = "删除失败" + ex.Message };
            }
        }
        /// <summary>
        /// 修改房号
        /// </summary>
        /// <param name="roomNumDto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ResultDto<bool>> UpdateAllAsync(CreateUpdateRoomNumDtos roomNumDto)
        {
            try
            {
                var roomTypeList = (await roomtyp.GetListAsync()).FirstOrDefault(m => m.RoomTypeName == roomNumDto.RoomTypeName);

                RoomNumber roomnum = ObjectMapper.Map<CreateUpdateRoomNumDtos, RoomNumber>(roomNumDto);

                roomnum.RoomTypeId = roomTypeList.Id;

                await roomRep.UpdateAsync(roomnum);

                return new ResultDto<bool> { Data = true, Code = StatusCode.Success, Msg = "修改成功" };
            }
            catch (Exception ex)
            {
                return new ResultDto<bool> { Data = false, Code = StatusCode.Faield, Msg = "修改失败" + ex.Message };
            }
        }
    }
}
